<?php
/**
 *（Logic）
 */
namespace App\Logic\Amazon\Api;

use SellingPartnerApi\Configuration;
class RequiredConfig
{
    private static $_instance = null;

    public static function getInit(): ?RequiredConfig
    {
        if(!is_object(self::$_instance)){
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    /**
     * @throws \Exception
     */
    public function configuration($refreshToken, $awsRegion, $configurationOptions = array()): Configuration
    {
        $options = array_merge(
            $configurationOptions,
            [
                "lwaClientId" => config('spapi.lwa_client_id'),
                "lwaClientSecret" => config('spapi.lwa_client_secret'),
                "lwaRefreshToken" => $refreshToken,
                "awsAccessKeyId" => config('spapi.aws_access_key_id'),
                "awsSecretAccessKey" => config('spapi.aws_secret_access_key'),
                "endpoint" => constant('SellingPartnerApi\Endpoint::'. $awsRegion),
                "roleArn" => config('spapi.role_arn'),
            ]
        );
        try{
            return new Configuration($options);
        }catch (\Exception $e){
            throw new $e;
        }
    }
}
